<?php
/**
 * 系统设置控制器类
 */
namespace Admin\Controller;
use Think\Controller;

class SystemController extends CommonController
{
    // 系统设置
    public function sys_set()
    {
        $post_data = I('post.data');
        if ($post_data) {
            switch ($post_data['type']) {
                case 'webInfo':
                    $copyright = M('sys_config')->where('cfg_name="copyright"')->setField('cfg_value', $post_data['copyright']);
                    $description = M('sys_config')->where('cfg_name="description"')->setField('cfg_value', $post_data['description']);
                    $title = M('sys_config')->where('cfg_name="title"')->setField('cfg_value', $post_data['title']);
                    if ($copyright || $description || $title) {
                        $info['type'] = 1;
                        $info['info'] = '保存成功';
                    } else {
                        $info['type'] = 0;
                        $info['info'] = '保存失败';
                    }
                    $this->ajaxReturn($info);
                    break;
                case 'smtp':
                    $smtp_host = M('sys_config')->where('cfg_name="smtp_host"')->setField('cfg_value', $post_data['smtp_host']);
                    $smtp_port = M('sys_config')->where('cfg_name="smtp_port"')->setField('cfg_value', $post_data['smtp_port']);
                    $smtp_pw = M('sys_config')->where('cfg_name="smtp_pw"')->setField('cfg_value', $post_data['smtp_pw']);
                    $smtp_user = M('sys_config')->where('cfg_name="smtp_user"')->setField('cfg_value', $post_data['smtp_user']);
                    if ($smtp_host || $smtp_port || $smtp_pw || $smtp_user) {
                        $info['type'] = 1;
                        $info['info'] = '保存成功';
                    } else {
                        $info['type'] = 0;
                        $info['info'] = '保存失败';
                    }
                    $this->ajaxReturn($info);
                    break;
                case 'webClose':
                    if (isset($post_data['open'])) {
                        $res = M('sys_config')->where('cfg_name="web_close"')->setField('cfg_value', '1');
                        if ($res) {
                            $info['type'] = 1;
                            $info['info'] = '保存成功';
                        } else {
                            $info['type'] = 0;
                            $info['info'] = '保存失败';
                        }
                        $this->ajaxReturn($info);
                    } else {
                        $res = M('sys_config')->where('cfg_name="web_close"')->setField('cfg_value', '0');
                        if ($res) {
                            $info['type'] = 1;
                            $info['info'] = '保存成功';
                        } else {
                            $info['type'] = 0;
                            $info['info'] = '保存失败';
                        }
                        $this->ajaxReturn($info);
                    }
                    break;
                default:
                    break;
            }
        } else {
            $this->log('系统设置', '', 1);
            $list = M('sys_config')->field('cfg_name,cfg_value')->where('id>=2 and id<=10')->select();
            foreach ($list as $v) {
                $info[$v['cfg_name']] = $v['cfg_value'];
            }
            $this->assign('info', $info);
            $this->display();
        }
    }
    // 数字字典
    public function sys_data()
    {
        $this->display();
    }
    // 屏蔽词
    public function sys_shield()
    {
        $this->display();
    }
    // 系统日志
    public function sys_log()
    {
        $table = 'log';
        if ($_POST['pid']) {//指定分页查询
            $get_post = I('post.pid');
            $path = MODULE_NAME.'/'.CONTROLLER_NAME.'/log_more';
            $log = $this->getPage($table, $get_post, $path, 10, true);
            foreach ($log['page_list'] as $key => $value) {
                $log['page_list'][$key]['time'] = timeFormat($value['time']);
            }
            $this->ajaxReturn($log);
        } elseif ($_POST['del']) {//删除操作
            $get_post = I('post.del');
            $table = 'log';
            $data = $this->deldata($table, $get_post);
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('系统日志ID', $log_info, 4);
            $this->ajaxReturn($data);
        } else {
            // $this->log('系统日志', null, 1);
            $log = $this->getPage($table, $get_post, '', 10, true);
            $this->assign('actname', ACTION_NAME);
            $this->assign('log_list', $log['page_list']);
            $this->assign('pid', $log['pid']);
            $this->assign('allpages', $log['allpages']);
            $this->assign('count', $log['count']);
            $this->display();
        }
    }
    // 系统日志更多内容展示页面
    public function log_more()
    {
        $get = I('get.');
        $more = M('log')->field('more')->where('id='.$get['id'])->find();
        $more = unserialize($more['more']);
        $this->assign('more', $more);
        // print_r($more);exit;
        $this->display();
    }
    // 系统日志检索
    public function sys_search()
    {
        layout(false);
        $get_post = I('post.data');
        $get_post['date_start'] = strtotime($get_post['date_start']);
        $get_post['date_stop'] = strtotime($get_post['date_stop']);
        if ($get_post['date_start'] && $get_post['date_stop']) {
            if ($get_post['date_start'] >= $get_post['date_stop']) {
                $info = '开始日期必须早于结束日期';
                $this->assign('info', $info);
                $this->display();
            } else {
                if ($get_post['username']) {
                    $sql = 'select * from log where time between '.$get_post['date_start'].' and '.$get_post['date_stop'].' and aname="'.$get_post['username'].'"';
                    $data = M()->query($sql);
                    if ($data) {
                        $this->assign('data', $data);
                        $this->display();
                    } else {
                        $info = '没有找到匹配的结果';
                        $this->assign('info', $info);
                        $this->display();
                    }
                } else {
                    $sql = 'select * from log where time between '.$get_post['date_start'].' and '.$get_post['date_stop'];
                    $data = M()->query($sql);
                    if ($data) {
                        $this->assign('data', $data);
                        $this->display();
                    } else {
                        $info = '没有找到匹配的结果';
                        $this->assign('info', $info);
                        $this->display();
                    }
                }
            }
        } else {
            $info = '必须填入开始日期和结束日期';
            $this->assign('info', $info);
            $this->display();
        }
    }
    // 友情链接
    public function sys_link()
    {
        if ($_POST['pid']) {//指定分页查询
            $get_post = I('post.pid');
            $table = 'links';
            $path = MODULE_NAME.'/'.CONTROLLER_NAME.'/link_edit';
            $data = $this->getPage($table, $get_post, $path);
            $this->ajaxReturn($data);
        } elseif ($_POST['del']) {//删除操作
            $get_post = I('post.del');
            $table = 'links';
            $data = $this->deldata($table, $get_post);
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('友情链接ID', $log_info, 4);
            $this->ajaxReturn($data);
        } elseif ($_POST['add']) {//添加操作
            $get_post = I('post.add');
            $table = 'links';
            $add = ['title'=>$get_post['name'], 'link'=>$get_post['link']];
            $data = $this->adddata($table, $add);
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('友情链接', $log_info, 2);
            $this->ajaxReturn($data);
        } elseif ($_POST['edit']) {//编辑操作
            $get_post = I('post.edit');
            $table = 'links';
            $edit = ['link_sort'=>$get_post['sort'], 'link'=>$get_post['link'], 'title'=>$get_post['name'], 'id'=>$get_post['id']];
            $data = $this->editdata($table, $edit);
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('友情链接', $log_info, 3);
            $this->ajaxReturn($data);
        } else {
            $link_list = $this->getPage('links');
            $this->assign('actname', ACTION_NAME);
            $this->assign('link_list', $link_list['page_list']);
            $this->assign('pid', $link_list['pid']);
            $this->assign('allpages', $link_list['allpages']);
            $this->assign('count', $link_list['count']);
            $this->display();
        }
    }
    // 友情链接编辑
    public function link_edit()
    {
        if ($_GET) {
            $get = I('get.id');
            $data = M('links')->where('id='.$get)->find();
            // var_dump($data);exit;
            $this->assign('data', $data);
            $this->assign('key', $get['key']);
            $this->display();
        }
    }

    // 第三方登录
    public function sys_qq()
    {
        // $this->log('第三方登录', '', 1);
        $cfg_name = 'qq_login';
        // 读取QQ登录开关信息
        $qq_switch = M('sys_config')->where('cfg_name="'.$cfg_name.'"')->field('cfg_value')->find();
        $qq_switch = $qq_switch['cfg_value'];
        // 读取QQ登录连接信息
        $qq_connect = file('ThinkPHP/Library/Vendor/QQConnect/API/comm/inc.php');
        $qq_connect = json_decode($qq_connect[1]);
        $appid = $qq_connect->appid;
        $appkey = $qq_connect->appkey;
        $callback = $qq_connect->callback;
        
        $this->assign('qq_switch', $qq_switch[0]);
        $this->assign('appid', $appid);
        $this->assign('appkey', $appkey);
        $this->assign('callback', $callback);
        $this->display();
    }

    // 写入第三方登录数据
    public function qqlogin_edit()
    {
        $post_data = I('post.data');
        // var_dump($post_data);
        // QQ登录开关配置
        $qq_switch_config = include 'Bbs/Home/Conf/config.php';// 读取前台配置文件
        // var_dump($qq_switch_config['QQ_SWITCH']);
        // 读取QQ登录连接信息
        $qq_connect = file('ThinkPHP/Library/Vendor/QQConnect/API/comm/inc.php');
        $qq_connect = json_decode($qq_connect[1]);
        // var_dump($qq_connect);
        if (array_key_exists('close', $post_data) && $post_data['close'] == 1) {
            // 收集更改数据
            $change_data['appid'] = $qq_connect->appid;
            $change_data['appkey'] = $qq_connect->appkey;
            $change_data['callback'] = $qq_connect->callback;
            // 新数据赋值
            $qq_connect->appid = $post_data['AppId'];
            $qq_connect->appkey = $post_data['Appkey'];
            $qq_connect->callback = $post_data['returnUrl'];
            $qq_switch_config['QQ_SWITCH'] = 1;
            // 把QQ登录连接信息写入配置文件中
            $qq_connect = json_encode($qq_connect);
            $qq_connect = "<?php die('forbidden'); ?>".PHP_EOL.$qq_connect;
            $qq_connect_edit = file_put_contents('ThinkPHP/Library/Vendor/QQConnect/API/comm/inc.php', $qq_connect);
            // 把QQ登录配置写入配置文件中
            $qq_switch_config = var_export($qq_switch_config, true);
            $qq_switch_config = '<?php'.PHP_EOL.'return '.$qq_switch_config.';'.PHP_EOL;
            $qq_switch_config_edit = file_put_contents('Bbs/Home/Conf/config.php', $qq_switch_config);
            // 读取数据库配置值
            $qq_switch = M('sys_config')->where('cfg_name="qq_login"')->field('cfg_value')->find();
            // 修改数据库配置值
            $data['cfg_value'] = 1;
            $qq_switch_edit = M('sys_config')->where('cfg_name="qq_login"')->data($data)->save();
            $change_data['close'] = $qq_switch_edit;
            if ($qq_switch['cfg_value'] == $post_data['close']) {
                if ($qq_connect_edit && $qq_switch_config_edit) {
                    $log_info = [0=>$post_data, 1=>$change_data];
                    $this->log('第三方登录', $log_info, 3);
                    $data = ['type'=>1,'info'=>'修改成功'];
                    $this->ajaxReturn($data);
                } else {
                    $data = ['type'=>0,'info'=>'修改失败'];
                    $this->ajaxReturn($data);
                }
            } else {
                if ($qq_connect_edit && $qq_switch_config_edit && $qq_switch_edit) {
                    $log_info = [0=>$post_data, 1=>$change_data];
                    $this->log('第三方登录', $log_info, 3);
                    $data = ['type'=>1,'info'=>'修改成功'];
                    $this->ajaxReturn($data);
                } else {
                    $data = ['type'=>0,'info'=>'修改失败'];
                    $this->ajaxReturn($data);
                }
            }
        } else {
            $qq_switch_config['QQ_SWITCH'] = 0;
            // 把QQ登录配置写入配置文件中
            $qq_switch_config = var_export($qq_switch_config, true);
            $qq_switch_config = '<?php'.PHP_EOL.'return '.$qq_switch_config.';'.PHP_EOL;
            $qq_switch_config_edit = file_put_contents('Bbs/Home/Conf/config.php', $qq_switch_config);
            // 修改数据库配置值
            $data['cfg_value'] = 0;
            $qq_switch_edit = M('sys_config')->where('cfg_name="qq_login"')->data($data)->save();
            $change_data['close'] = $qq_switch_edit;
            if ($qq_switch_config_edit && $qq_switch_edit) {
                $log_info = [0=>$post_data, 1=>$change_data];
                $this->log('第三方登录', $log_info, 3);
                $data = ['type'=>1,'info'=>'修改成功'];
                $this->ajaxReturn($data);
            } else {
                $data = ['type'=>0,'info'=>'修改失败'];
                $this->ajaxReturn($data);
            }
        }
    }
}